package com.supermarket.model;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;

/**
 * 促销活动实体类
 * 对应数据库中的promotions表
 */
public class Promotion {
    private int id;
    private String promotionCode;
    private String promotionName;
    private String promotionType; // DISCOUNT, FIXED_AMOUNT, BUY_GET
    private BigDecimal discountRate; // 折扣率（0-100）
    private BigDecimal discountAmount; // 立减金额
    private int minQuantity; // 最小购买数量
    private Date startDate;
    private Date endDate;
    private String status; // PENDING, ACTIVE, EXPIRED, CANCELLED
    private int createdBy;
    private Timestamp createdAt;
    private Timestamp updatedAt;

    // 显示用字段（从关联表中获取）
    private String createdByName; // 创建人姓名

    // 构造函数
    public Promotion() {
        this.minQuantity = 1;
        this.status = "PENDING";
    }

    public Promotion(String promotionCode, String promotionName, String promotionType) {
        this();
        this.promotionCode = promotionCode;
        this.promotionName = promotionName;
        this.promotionType = promotionType;
    }

    // Getter和Setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPromotionCode() {
        return promotionCode;
    }

    public void setPromotionCode(String promotionCode) {
        this.promotionCode = promotionCode;
    }

    public String getPromotionName() {
        return promotionName;
    }

    public void setPromotionName(String promotionName) {
        this.promotionName = promotionName;
    }

    public String getPromotionType() {
        return promotionType;
    }

    public void setPromotionType(String promotionType) {
        this.promotionType = promotionType;
    }

    public BigDecimal getDiscountRate() {
        return discountRate;
    }

    public void setDiscountRate(BigDecimal discountRate) {
        this.discountRate = discountRate;
    }

    public BigDecimal getDiscountAmount() {
        return discountAmount;
    }

    public void setDiscountAmount(BigDecimal discountAmount) {
        this.discountAmount = discountAmount;
    }

    public int getMinQuantity() {
        return minQuantity;
    }

    public void setMinQuantity(int minQuantity) {
        this.minQuantity = minQuantity;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(int createdBy) {
        this.createdBy = createdBy;
    }

    public Timestamp getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Timestamp createdAt) {
        this.createdAt = createdAt;
    }

    public Timestamp getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Timestamp updatedAt) {
        this.updatedAt = updatedAt;
    }

    public String getCreatedByName() {
        return createdByName;
    }

    public void setCreatedByName(String createdByName) {
        this.createdByName = createdByName;
    }

    /**
     * 获取促销类型的中文描述
     */
    public String getPromotionTypeDescription() {
        switch (promotionType) {
            case "DISCOUNT":
                return "折扣";
            case "FIXED_AMOUNT":
                return "满减";
            case "BUY_GET":
                return "买赠";
            default:
                return promotionType;
        }
    }

    /**
     * 获取促销状态的中文描述
     */
    public String getStatusDescription() {
        switch (status) {
            case "PENDING":
                return "待启动";
            case "ACTIVE":
                return "进行中";
            case "EXPIRED":
                return "已结束";
            case "CANCELLED":
                return "已取消";
            default:
                return status;
        }
    }

    /**
     * 获取优惠规则描述
     */
    public String getPromotionRule() {
        if ("DISCOUNT".equals(promotionType) && discountRate != null) {
            return String.format("%.1f折", discountRate.divide(new BigDecimal("10")));
        } else if ("FIXED_AMOUNT".equals(promotionType) && discountAmount != null) {
            if (minQuantity > 1) {
                return String.format("满%d件立减%.0f元", minQuantity, discountAmount);
            } else {
                return String.format("立减%.0f元", discountAmount);
            }
        } else if ("BUY_GET".equals(promotionType) && minQuantity > 1) {
            return String.format("买%d送1", minQuantity);
        }
        return "详见活动说明";
    }

    /**
     * 判断促销活动是否有效（当前时间在活动期间内且状态为ACTIVE）
     */
    public boolean isActive() {
        if (!"ACTIVE".equals(status)) {
            return false;
        }

        java.util.Date now = new java.util.Date();
        return startDate != null && endDate != null &&
                !now.before(startDate) && !now.after(endDate);
    }

    /**
     * 计算促销折扣金额
     * 
     * @param originalAmount 原价金额
     * @param quantity       购买数量
     * @return 折扣金额
     */
    public BigDecimal calculateDiscountAmount(BigDecimal originalAmount, int quantity) {
        if (!isActive() || quantity < minQuantity) {
            return BigDecimal.ZERO;
        }

        switch (promotionType) {
            case "DISCOUNT":
                if (discountRate != null) {
                    return originalAmount.multiply(
                            new BigDecimal("100").subtract(discountRate).divide(new BigDecimal("100")));
                }
                break;
            case "FIXED_AMOUNT":
                if (discountAmount != null) {
                    return discountAmount;
                }
                break;
            case "BUY_GET":
                // 买赠类型的计算逻辑，这里简化处理
                int freeQuantity = quantity / (minQuantity + 1);
                BigDecimal unitPrice = originalAmount.divide(new BigDecimal(quantity), 2,
                        java.math.RoundingMode.HALF_UP);
                return unitPrice.multiply(new BigDecimal(freeQuantity));
        }

        return BigDecimal.ZERO;
    }

    @Override
    public String toString() {
        return "Promotion{" +
                "id=" + id +
                ", promotionCode='" + promotionCode + '\'' +
                ", promotionName='" + promotionName + '\'' +
                ", promotionType='" + promotionType + '\'' +
                ", discountRate=" + discountRate +
                ", discountAmount=" + discountAmount +
                ", minQuantity=" + minQuantity +
                ", startDate=" + startDate +
                ", endDate=" + endDate +
                ", status='" + status + '\'' +
                ", createdBy=" + createdBy +
                '}';
    }
}
